/*
 *  Puppeteer example
 *   启动 chrome
 */

const puppeteer = require('puppeteer');

(async() => {
	// 默认启动内嵌的 chrome
	const browser = await puppeteer.launch(
		{args: ['--no-sandbox']}
	);
	//  启动系统的 chrome
	//const browser = await puppeteer.launch(
        //        {args: ['--no-sandbox'],
	//	 executablePath: 'google-chrome'
	//	}
        //);
	console.log(await browser.version());

	// 提取 dom
	const page = await browser.newPage();
	await page.goto('http://www.hiqu.me', {waitUntil: 'networkidle2'});
	const title = await page.title();
	console.log(`Page Title: ${title}`)
	const titleHdl = await page.$x('//head/title/text()');
	const title2 = await page.evaluate(e=>e.textContent, titleHdl[0])
	//const content = await page.content();
	//console.log(content)
	console.log(`Page Title: ${title2}`)

	await browser.close();
})();
